using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Argotic.Core.Rss;
using Argotic.Extensions;
using Argotic.Extensions.Social;

namespace Blog.Importer
{
  public class RssReader : BaseReader
  {
    #region Constructors
    public RssReader() : base()
    {

    }
    #endregion

    #region Locals & Properties

    string _rssPath;

    public string RssPath
    {
      set { _rssPath = value; }
    }

    #endregion

    public override void Import()
    {
      RssFeed doc;
      try
      {
        doc = RssFeed.Create(new Uri(_rssPath));
      }
      catch (Exception ex)
      {
        throw new Exception("Rss Load Failed.", ex);
      }

      WebImporter.BlogImporter service;
      try
      {
        service = BlogWriter.GetService(_blogPath, _userName, _password);
      }
      catch (Exception ex)
      {
        throw new Exception("BlogEngine Web Service Connection Failed.", ex);
      }

      foreach (RssItem item in doc.Channel.Items)
      {
        WebImporter.ImportPost import = new WebImporter.ImportPost();
        import.Title = item.Title;
        if (item.Author != null)
          import.Author = item.Author.ToString();
        import.PostDate = item.PublicationDate;
        import.Content = BlogWriter.FilterContent(item.Description, service, _blogPath, _fileOption, _filePath);
        import.Description = "";
        import.Publish = true; 

        // Categories
        List<string> categories = new List<string>();
        foreach (RssCategory cat in item.Categories)
        {
          categories.Add(cat.Value);
        }
        import.Categories = categories.ToArray();

        // Tags not included :(

        // Save Post
        string postID = service.AddPost(import, item.Link.AbsoluteUri, _removeDups);

        // Save Comments & Trackbacks
        foreach (SyndicationExtension extension in item.Extensions.Values)
        {
          if (extension.GetType() == typeof(WellFormedWebCommentsExtension))
          {
            // if we have an RSSComment feed, let's try to get them
            WellFormedWebCommentsExtension wfw = (WellFormedWebCommentsExtension)extension;
            
            try
            {
              RssFeed comments = RssFeed.Create(wfw.Feed);
              foreach (RssItem comment in comments.Channel.Items)
              {
                service.AddComment(postID, comment.Author.ToString(), "", "", comment.Description, comment.PublicationDate);
              }
            }
            catch (Exception ex)
            {
              string temp = ex.Message;
              // Silent... Comments are secondary for now.
            }
            

          }
          
        }


      }


    }

    public override bool Validate()
    {
      bool response = true;

      try
      {
        RssFeed feed = RssFeed.Create(new Uri(_rssPath));
      }
      catch (Exception ex)
      {
        response = false;
      }

      return response;
    }



  }
}
